package info.niwota.ezb;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
public class ShortcutActivity extends Activity {

	private static final String TAG = "ShortcutActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onCreate " + savedInstanceState);
		}
		super.onCreate(savedInstanceState);
		//
		final Intent i = getIntent();
		final String action = i.getAction();
		if (Debug.DEBUG) {
			Log.d(TAG, "onCreate intent: " + i);
		}
		if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
			setupShortcut(this, ImageHolderActivity.class,
					getString(R.string.app_name), R.drawable.icon);
		}
		finish();
	}

	private static Intent createShortcutIntent(Context ctx, String name,
			int icon, Intent shortcutIntent) {
		Intent i = new Intent();
		i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		i.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
		Parcelable iconRes = Intent.ShortcutIconResource.fromContext(ctx, icon);
		i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
		return i;
	}

	private static void setupShortcut(Activity ctx, Class<?> cls, String name,
			int icon) {
		Intent si = new Intent(ctx, cls);
		Intent intent = createShortcutIntent(ctx, name, icon, si);
		ctx.setResult(Activity.RESULT_OK, intent);
	}
}
